package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.SchedulQueryDto;
import com.csun.cmny.provider.model.RoomAndNursingVo;
import com.csun.cmny.provider.model.domain.OmcRoomAssignment;
import com.csun.cmny.provider.model.domain.OmcSchedul;
import com.csun.cmny.provider.model.dto.RoomAssignmentListDto;
import com.csun.cmny.provider.model.dto.RoomAssignmentQueryDto;
import com.csun.cmny.provider.pojo.Room;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcRoomAssignmentService;
import com.csun.cmny.provider.validator.RoomAssignmentListAddValid;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
 * 前端先根据NursinglogController的/selectByDate接口判断，当该接口返回的不为0时才可以进行房间分配的操作
 * */
@RestController
@RequestMapping(value = "/roomAssignment", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcRoomAssignmentController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcRoomAssignmentService omcRoomAssignmentService;


    /**
     * 保存房间分配
     *
     * @param: [roomAssignmentListDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation
    @PostMapping(value = "/save")
    public Wrapper roomAsSave(@Validated(RoomAssignmentListAddValid.class)
                               @RequestBody RoomAssignmentListDto roomAssignmentListDto,
                               BindingResult br) {

        List<OmcRoomAssignment> roomAssList = roomAssignmentListDto.getRoomAssList();

        for(OmcRoomAssignment omc : roomAssList){
            if(omc.getRoomId() == null){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                        .getMessage("RoomAssignmentList.roomId.null", null));
            }
            if(omc.getUserId() == null){
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                        .getMessage("RoomAssignmentList.userId.null", null));
            }
        }

        int ret = omcRoomAssignmentService.save(roomAssignmentListDto,getLoginAuthDto());

        if(ret < 1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),ret);
    }

    /**
     * 查询房间分配——一进入页面的显示
     * @param: [roomAssignmentQueryDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation
    @PostMapping(value = "/selectAll")
    public Wrapper queryRoomList(@RequestBody RoomAssignmentQueryDto roomAssignmentQueryDto,
                                 BindingResult br) {

        OmcRoomAssignment omcRoomAssignment= new OmcRoomAssignment();
        BeanUtils.copyProperties(roomAssignmentQueryDto,omcRoomAssignment);

        List<Room> roomList = omcRoomAssignmentService.queryRoomAssList(omcRoomAssignment,getLoginAuthDto());

        //return WrapMapper.ok(roomList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),roomList);
    }

    /**
     * 查询房间分配——弹窗显示对应班次下的人对应的房间
     * 周一至星期天时间只能有一个有数据
     * 班次集合——>负责人集合——>房间集合
     *
     *  @param: [schedulQueryDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation
    @PostMapping(value = "/select")
    public Wrapper queryScheduleList(@Validated(RoomAssignmentListAddValid.class)
                                @RequestBody SchedulQueryDto schedulQueryDto,
                                     BindingResult br) {

        OmcSchedul omcSchedul = new OmcSchedul();
        BeanUtils.copyProperties(schedulQueryDto,omcSchedul);

        List<OmcSchedul> omcScheduleList = omcRoomAssignmentService.queryScheduleAssList(omcSchedul,schedulQueryDto.getDate(),getLoginAuthDto());
        //return WrapMapper.ok(omcScheduleList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcScheduleList);
    }

    /**
     * 签到删除
     * @param: [roomAssignmentId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
  //  @LogAnnotation
    @PostMapping(value = "/deleteById/{roomAssignmentId}")
    public Wrapper roomAsSave(@PathVariable Long roomAssignmentId) {

        // 校验Id是否为空
        Preconditions.checkArgument(roomAssignmentId != null, cmnyMessageSource
                .getMessage("roomAssignment.id.null", null));
        Integer result = omcRoomAssignmentService.deleteByKey(roomAssignmentId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 月排班签到
      * @param schedulQueryDto
     * @return null
     * @author HD
     * @date  20:06
     */
    @PostMapping("/selectByMonth")
    public Wrapper selectByMonth(@RequestBody SchedulQueryDto schedulQueryDto){
        OmcSchedul omcSchedul = new OmcSchedul();
        BeanUtils.copyProperties(schedulQueryDto,omcSchedul);

        RoomAndNursingVo omcScheduleList = omcRoomAssignmentService.queryScheduleMonth(omcSchedul,schedulQueryDto.getDate(),getLoginAuthDto());
        //return WrapMapper.ok(omcScheduleList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),omcScheduleList);
    }

}
